<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Request;
use think\Db;

class Partner extends Base
{
    public $add;
    public $check;
    public $del;
    public $update;
    public function __construct()
    {
        parent::__construct();
        $power = $this->checkPower('合作伙伴');
        if(empty($power)){
            $this->add = 0;
            $this->check =0;
            $this->update =0;
            $this->del =0;
        }else{
            $this->add = $power[0]['padd'];
            $this->check = $power[0]['pcheck'];
            $this->update = $power[0]['pupdate'];
            $this->del = $power[0]['pdel'];
        }
        
    }

    public function index()
    {
        return $this->fetch('list');
    }

    public function newsList($key='')
    {
        //获取每页显示条数
        $limit = Request::instance()->param('limit') ? Request::instance()->param('limit') : 5;
        //获取当前页数
        $page = Request::instance()->param('page') ? Request::instance()->param('page') : 1;
        //计算偏移量
        $offset = ($page - 1) * $limit;

        if($key == ''){
            $list = Db::name('friends')->select();
            $count = count($list);
            //查询当前页显示的数据
            $case = Db::name('friends')->limit($offset, $limit)->select();
        }else{
            // 如果有 关键字 就使用模糊查询
            $list = Db::name($key['table'])->where("{$key['condition']}", 'like', "%{$key['key']}%")
                                    ->select();
            $count = count($list);
            $case = Db::name($key['table'])->where("{$key['condition']}", 'like', "%{$key['key']}%")
                                    ->limit($offset, $limit)
                                    ->select();
        }
        
        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'limit' => $limit,
            'data' => $case,
        ];

        return json($data);
    }

    // 渲染搜索到的页面信息
    public function search($key){
        return $this->newsList($key);
    }

    // 渲染新闻add页面
    public function add()
    {
        return $this->fetch('add');
    }

    //添加或更新
    public function insert()
    {
        $data = input('post.data/a');
        $id = input('post.data.fid/a')[0];
        $table = input('post.table');
        $data = array_filter($data);
        if (!$id) {
            if($this->add != 1){
                return json(['code' => 3, 'msg' => '您没有这权限！']);
            }

            $res = Db::name($table)->insert($data);
            if ($res) {
                return json([
                    'code' => 0,
                    'msg' => '添加成功!',
                ]);
            } else {
                return json([
                    'code' => 1,
                    'msg' => '添加失败!',
                ]);
            }
        } else {
            if($this->update != 1){
                return json(['code' => 3, 'msg' => '您没有这权限！']);
            }

            $res = Db::name($table)->where('fid', $id)->update($data);
            if ($res) {
                return json([
                    'code' => 0,
                    'msg' => '修改成功!',
                ]);
            } else {
                return json([
                    'code' => 1,
                    'msg' => '修改失败!',
                ]);
            }
        }
    }

    // 删除
    public function del($mid,$table,$condition)
    {
        if($this->del != 1){
            return json(['code' => 3, 'msg' => '您没有这权限！']);
        }

        $img = input('get.img_url');
        if($img != ''){
            if($img[0] == '/'){
                $img = substr($img, 1);
            }
            if(file_exists($img)){
                @unlink($img);
            }
        }
        $res = Db::name($table)->where($condition, 'in', $mid)->delete();
        if ($res) {
            return json([
                'code' => 0,
                'msg' => '删除成功！',
            ]);
        } else {
            return json(
                [
                    'code' => 1,
                    'msg' => '删除失败！',
                ]
            );
        }
    }
}
